from rest_framework import serializers
from .models import Host, HostGroup, Project, Environment, User, UserGroup, Recipient


class HostGroupSerializer(serializers.ModelSerializer):
    host_count = serializers.IntegerField(source='hosts.count', read_only=True)
    
    class Meta:
        model = HostGroup
        fields = '__all__'


class ProjectSerializer(serializers.ModelSerializer):
    environment_count = serializers.IntegerField(source='environments.count', read_only=True)
    
    class Meta:
        model = Project
        fields = '__all__'


class EnvironmentSerializer(serializers.ModelSerializer):
    project_name = serializers.CharField(source='project.name', read_only=True)
    host_count = serializers.IntegerField(source='hosts.count', read_only=True)
    
    class Meta:
        model = Environment
        fields = '__all__'


class HostSerializer(serializers.ModelSerializer):
    host_group_name = serializers.CharField(source='host_group.name', read_only=True, allow_null=True)
    environment_name = serializers.CharField(source='environment.name', read_only=True, allow_null=True)
    server_type_display = serializers.CharField(source='get_server_type_display', read_only=True)
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    
    class Meta:
        model = Host
        fields = '__all__'
        extra_kwargs = {
            'password': {'write_only': True},
            'ssh_key': {'write_only': True},
        }


class UserGroupSerializer(serializers.ModelSerializer):
    user_count = serializers.IntegerField(source='users.count', read_only=True)
    
    class Meta:
        model = UserGroup
        fields = '__all__'


class UserSerializer(serializers.ModelSerializer):
    user_group_name = serializers.CharField(source='user_group.name', read_only=True, allow_null=True)
    
    class Meta:
        model = User
        fields = '__all__'


class RecipientSerializer(serializers.ModelSerializer):
    class Meta:
        model = Recipient
        fields = '__all__'
